#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
# 
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998
# Netscape Communications Corporation. All Rights Reserved.
# 
# Contributor(s): TomWij <TomWij@live.com>

##############################################################################
#
# testStatistics.cgi
# ---------------------
# Test whether that statistics work.
#
##############################################################################

use DBI;
use DBD::mysql;

use Test::More tests => 3;

use LWP::UserAgent;
my $ua = new LWP::UserAgent;

### Set-up ###
# Empty the bugs, statistics and model of the Test Component of our Test Product. Then recreate a new model.
do '.\emptyDatabase.cgi';

### Tests ###
# Check if there are initially no statistics.
$response = $ua->post('http://localhost/extensions/BugPrediction/lib/admin/getStatistics.cgi', {});
$content = $response->content;
ok($content !~ m/product=TestProduct/im, 'Check if there are initially no statistics in our test product.');

# Add a suggestion statistic.
$response = $ua->post('http://localhost/extensions/BugPrediction/lib/admin/addStatistic.cgi', {user => 'enhancement', suggested => 'trivial', choice => 'enhancement'});
$content = $response->content;

# Connect to the MySQL database and update the product id and component id of the suggestion.
$db_name = 'bugs';
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';

$driver = DBI->install_driver("mysql"); 
$connect = $driver->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, {}) or die "MySQL: Connection failed";

$query = "UPDATE suggestions SET pid = 'TestProduct', cid = 'TestComponent' WHERE bid = '-1';";
$query_handle = $connect->prepare($query) or die "<b>MySQL: Query Preparation failed.</b>";
$query_handle->execute() or die "<b>MySQL: Query Execution failed.</b>";

# Check if there are statistics now.
$response = $ua->post('http://localhost/extensions/BugPrediction/lib/admin/getStatistics.cgi', {});
$content = $response->content;
ok($content =~ m/product=TestProduct/im, 'Check if there are statistics in our test product when we add them.');
ok($content =~ m/<td>enhancement<\/td><td>trivial<\/td><td>enhancement<\/td>/im, 'Check if our test user has chosen "enhancement", but "trivial" was suggested and the user has chosen "enhancement"');